package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.dao.LoginLogMapper;
import com.stylefeng.guns.common.persistence.model.LoginLog;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.service.ILoginLogService;
import com.stylefeng.guns.modular.system.warpper.LoginLogWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * channel控制器
 *
 * @author fengshuonan
 * @Date 2018-01-08 20:29:57
 */
@Controller
@RequestMapping("/loginLog")
public class LoginLogController extends BaseController {

    private String PREFIX = "/system/loginLog/";

    @Autowired
    private ILoginLogService loginLogService;

    @Resource
    private LoginLogMapper loginLogMapper;

    /**
     * 跳转到channel首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "loginLog.html";
    }

    /**
     * 跳转到添加channel
     */
    @RequestMapping("/loginLog_add")
    public String loginLogAdd() {
        return PREFIX + "loginLog_add.html";
    }

    /**
     * 跳转到修改channel
     */
    @RequestMapping("/loginLog_update/{loginLogId}")
    public String loginLogUpdate(@PathVariable Integer loginLogId, Model model) {
        LoginLog loginLog = loginLogService.selectById(loginLogId);
        model.addAttribute("item", loginLog);
        LogObjectHolder.me().set(loginLog);
        return PREFIX + "loginLog_edit.html";
    }

    /**
     * 获取channel列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String beginTime,
                       @RequestParam(required = false) String endTime,
                       @RequestParam(required = false) String condition) {
        Page<LoginLog> page = new PageFactory<LoginLog>().defaultPage();
        List<Map<String, Object>> result = loginLogMapper.list(page, beginTime, endTime, condition, page.getOrderByField(), page.isAsc());
        page.setRecords((List<LoginLog>) new LoginLogWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增channel
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(LoginLog loginLog) {
        loginLogService.insert(loginLog);
        return SUCCESS_TIP;
    }

    /**
     * 删除channel
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer loginLogId) {
        loginLogService.deleteById(loginLogId);
        return SUCCESS_TIP;
    }

    /**
     * 修改channel
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(LoginLog loginLog) {
        loginLogService.updateById(loginLog);
        return SUCCESS_TIP;
    }

    /**
     * channel详情
     */
    @RequestMapping(value = "/detail/{loginLogId}")
    @ResponseBody
    public Object detail(@PathVariable("loginLogId") Integer loginLogId) {
        return loginLogService.selectById(loginLogId);
    }
}
